Beginners Programming
Demos
The purpose of this discussion is not to spend a lot of time on actual code, but to highlight the advantages that Random Access Files have over Sequential Files and a trick or two that you can do with Random Access Files that are all but impossible with Sequential Files. You can find excellent articles on coding Random Access files in Newsletters 9, 10 and 12 and a good article on indexing in Newsletter 11. These articles will show you how to add, change and delete from a Random Access file.
What is the difference between random access and sequential? Either type of file can be opened using Notepad or Wordpad since they are really just files containing text. If you had the following information in either file, it might physically look like this:
Sequential: Each line is one record with one data field of any length.
Brown John 5 State Street Dover MD 11198
Random: Each line is one record with multiple data fields each of a specified length.
Brown John 5 State Street Dover MD11198
In reality, in a Random Access file, all the records are strung together into one long string of some total number of bytes.
The real advantage of random access files is as their name implies, once they are opened, they can be read from or written to in a random manner just by using a record number or you can add to the end since you will know how many records are in the file. The following code tells you all you need to know about a random access file.
Open “filename.ext” for random as #filehandle len = 77
Field #filehandle,15 as lastname$,15 as firstname$, 20 as address$,_
15 as city$, 2 as State$, 10 as zip$
NumberOfRecords=lof(#filehandle)/77
First we open the file as a random file which can now be read, changed or appended with a length of each record of 77 bytes. Next we define the length of each piece of data in each record and assign a field variable name to each piece. In the final line, the ‘lof’ or length of file, is the total number of bytes in the file. So if we divide lof by the length of each record, we get the total number of records in the file.
To read a record we use ‘get #filehandle, recordnumber” and the field variables we defined in the Field statement will contain the data. These variables are global to a program, even if opened inside a subroutine and need not be defined as Global. The NumberOfRecords is not global and must be defined as global or otherwise handled. One way to do this is to use an array with one element such as NumberOfRecords(1). To place information into the record, we get the data into each field variable from a text box, input statements, array or other method and use ‘put #filehandle, recordnumber’. Of course, if we are adding data to the end of the file, we would index NumberOf Records by 1 and then use ‘put #filehandle, NumberOfRecords’.
Retrieving all the records from a file and placing all the names into an array looks like:
for recordnumber=1 to NumberOfRecords
get #filehandle, recordnumber
array$(recordnumber) = firstname$ + ” “ + lastname$
next
So now we can see that one big advantage of Random Access files is once they are open, we can write, read or append without having to reopen the file in a different mode. Another advantage which you never hear about is, once you have opened a random file and have defined the fields, there may be an occasion when you want to get all the fields from a record into one variable. Of course one way is to retrieve each record and then concatenate all the field variables into one string such as a$=lastname$+firstname$+address+ etc. Or you can redefine the field on the fly to look like:
Field #filehandle, 77 as a$
and you’re done.
When you use the ‘get #filename, recordnumber’, each field is retrieved to its full length. That means that lastname$ will always 15 bytes long. Remember that the length of each field should be at least as long as the longest piece of data you expect to use. In some cases leaving the variable full length is ok. But suppose you want to display the full name as “Brown, John”. One way would to use ‘a$=trim$(lastname$) + ”, “ + trim$(firstname$)’. Fortunately Liberty Basic has a quicker alternative. Use ‘gettrim #filehandle, recordnumber’ instead and each field will automatically be trimmed so all you need to do is add the fields together.
When should you use Sequential or Random Access file? Sequential files lend themselves very nicely to small files like initialization files, where you might store program start up information such as font name, size and style or game save files where you have number of lives, weapons etc., and each game save file will have a different game or player name. They also lend themselves to large numbers of text lines such as a document where each line is of a different length.
Random access files make their living by storing small or large amounts of data where each record contains the same information. Data like name, address, zip, telephone number, number of kids or record label, artist, length of play. Records like these are a lot easier to manipulate when retrieved from a random file than a sequential file. Each data field doesn’t have to contain data, for example, if you don’t have the phone number for a person, that data field would be blank. The space in the file is still there and you can fill it at any time without having to re write the entire file.
Speaking of space, over the years I have found it very advantageous to have extra space in each record. Thirty percent isn’t always too much. Somewhere down the road, you might think of another field you wished you had in each record. Using the original example modified for 100 bytes per record:
Open “filename.ext” for random as #filehandle len = 100
Field #filehandle,15 as lastname$,15 as firstname$, 20 as address$,_
15 as city$, 2 as State$, 10 as zip$, 23 as dummy$
NumberOfRecords=lof(#filehandle)/100
It’s easy to see that if you need to add a numeric field that is 5 bytes long, all you need to do is change the Field statement to look like:
Field #filehandle,15 as lastname$,15 as firstname$, 20 as address$,_
15 as city$, 2 as State$, 10 as zip$, 5 as number, 18 as dummy$
Remember, Random Access files are your friend. If you are going to store large amounts of repetitive data where each record, for the most part, will contain the same kind of information you going to want to use random access files. If you are going to have an index of some data field in a list box and want the user to select an item from the list box and then retrieve the remainder of the information that goes with the selected item, you don’t want to have to search through a sequential file. You want to read only the record associated with the selected item. If you haven’t used Random access files because they looked complicated, please read Newsletters 9, 10 and 12. Look at the Liberty Basic help file for Random Access Files. When you reach LOF, you’ll be glad you did.